package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Homework;
import com.example.demo.mapper.HomeWorkMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/homework")
public class HomeWorkController {

    @Resource
    HomeWorkMapper homeworkMapper;


    @PostMapping
    public Result<?> save(@RequestBody Homework homework) {

        homeworkMapper.insert(homework);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody Homework homework) {
        homeworkMapper.updateById(homework);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        homeworkMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<Homework> wrapper = Wrappers.<Homework>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Homework::getWorkName, search);
        }
        Page<Homework> stuPage = homeworkMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }
}